package com.pks.datacenter.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class SUtilAes {

	private static final Logger logger = LoggerFactory.getLogger(SUtilAes.class);

	/** AES 加解密密钥，请勿擅自修改！！！ */
//	private static final String key = "0000000000000000";
	private static final String key = "ohedudatacenter0";

	/**
	 * AES 加密 使用AES-128-ECB加密模式
	 * 
	 * @param sSrc
	 *            需要加密的字段
	 * @param sKey
	 *            16 位密钥
	 * @return
	 * @throws Exception
	 */
	public static String Encrypt(String sSrc, String sKey) {
		try {
			if (sKey == null) {
				logger.info("Key为空null");
				return null;
			}
			/** 判断Key是否为16位 */
			if (sKey.length() != 16) {
				logger.info("Key长度不是16位");
				return null;
			}
			byte[] raw = sKey.getBytes("utf-8");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			/** "算法/模式/补码方式" */
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
			byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
			/** 此处使用BASE64做转码功能，同时能起到2次加密的作用。 */
//			return new Base64Encoder().encode(encrypted);
			return new Base64().encodeToString(encrypted);
		} catch (Exception e) {
			logger.info(sSrc + " AES解密异常！");
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * AES 加密 使用AES-128-ECB加密模式 使用定义好的加密key
	 * 
	 * @param sSrc
	 *            需要加密的字段
	 * @return
	 * @throws Exception
	 */
	public static String Encrypt(String sSrc) {
		return Encrypt(sSrc, key);
	}

	/**
	 * AES 解密 使用AES-128-ECB加密模式
	 * 
	 * @param sSrc
	 *            需要解密的字段
	 * @param sKey
	 *            16 位密钥
	 * @return
	 * @throws Exception
	 */
	public static String Decrypt(String sSrc, String sKey) {
		try {
			// 判断Key是否正确
			if (sKey == null) {
				logger.info("Key为空null");
				return null;
			}
			// 判断Key是否为16位
			if (sKey.length() != 16) {
				logger.info("Key长度不是16位");
				return null;
			}
			byte[] raw = sKey.getBytes("utf-8");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, skeySpec);
			/** 先用base64解密 */
			byte[] encrypted1 = new Base64().decode(sSrc);
			try {
				byte[] original = cipher.doFinal(encrypted1);
				String originalString = new String(original, "utf-8");
				return originalString;
			} catch (Exception e) {
				System.out.println(e.toString());
				return null;
			}
		} catch (Exception e) {
			logger.info(sSrc + " AES解密异常！");
			e.printStackTrace();
			return null;
		}
	}

	public static String Decrypt(String sSrc) {
		return Decrypt(sSrc, key);
	}
	
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("15957796808");
		list.add("13004759601");
		list.add("13017827683");
		list.add("13017867659");
		list.add("13017887790");
		list.add("13017898315");
		list.add("13017898902");
		list.add("13057707211");
		list.add("13057807727");
		list.add("13057932281");
		list.add("13057961605");
		list.add("13057989820");
		list.add("13057994391");
		list.add("13057995270");
		list.add("13075733268");
		list.add("13075752938");
		list.add("13075775565");
		list.add("13088650650");
		list.add("13088688018");
		list.add("13091963202");
		list.add("18257753968");
		for (String a : list) {
			String encrypt_str = Encrypt(a,"jkl;POIU1234++==");
			System.out.println(encrypt_str);
		}
		
//		String a = "330321196804130348";
//		
//		String decrypt_str = Decrypt(encrypt_str,"jkl;POIU1234++==");
//		System.out.println(decrypt_str);
		
	}
	
}
